﻿using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.OleDb;
using QLQTPM_ThuDuc_Group7.DTO;

namespace DesignLayOut_QLQTPM_Team7.Data
{
    public class TenDiaDiemRespository
    {
        private string connectString;
        public TenDiaDiemRespository()
        {
            connectString = ConfigurationSettings.AppSettings["ConnectionString"];
            string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            connectString = connectString.Replace("|DataDirectory|", path);
        }
        public int ThemTenDiaDiem(TenDiaDiemDTO tendiadiem) {
            int id = 0;
            try
            {
                OleDbConnection cnn = new OleDbConnection(connectString);
                OleDbCommand objCmd = new OleDbCommand("Insert into tendiadiem(tendiadiem)"
                + "values (@tendiadiem)", cnn);
                //objCmd.Parameters.Add("@matendiadiem", OleDbType.Integer).Value = tendiadiem.matendiadiem;
                objCmd.Parameters.Add("@tendiadiem", OleDbType.VarWChar).Value = tendiadiem.TenDiaDiem;
                cnn.Open();

                int n = objCmd.ExecuteNonQuery();
                if (n == 1)
                {
                    objCmd = new OleDbCommand("select @@Identity as ID", cnn);
                    OleDbDataReader reader = objCmd.ExecuteReader();
                    if (reader.Read())
                    {
                        id = (int)reader.GetValue(reader.GetOrdinal("ID"));
                    }
                    
                }
                                
                cnn.Close();
            }
            catch (Exception ex)
            {
                //Log4Net
                Console.WriteLine(ex.Message);
            }
            return id;
        }

    }
}
